<?php

	class DB 
	{
		private $conn;
		public $error = '';

		public function __construct() 
		{
			$this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASS, true);
			if(!$this->conn)
			{
			    $this->error = mysql_error();
			}
			
			if(!mysql_select_db(DB_NAME, $this->conn))
			{
			    $this->error = mysql_error();
			}
		}

		public function table($table) 
		{
			return DB_PREF . $table;
		}

		public function query($query) 
		{
			$res = mysql_query($query, $this->conn);
			
			if(!$res)
			{
			    $this->error = mysql_error();
			    return false;
			}

			if(preg_match('/^(insert|delete|update|replace)\s+/i', $query)) 
			{
				$obj = new stdClass();
				$obj->affected_rows = mysql_affected_rows($this->conn);
				$obj->insert_id = mysql_insert_id($this->conn);
				return $obj;
			} 
			else 
			{
				return new DB_Result($res);
			}
		}
	}

	class DB_Result 
	{
		private $res;
		public $num_rows;

		public function DB_Result($res) 
		{
			$this->res = array();

			// store the results
			$i = 0;
			while($row = mysql_fetch_object($res)) 
			{
				$this->res[$i] = strip($row);
				$i++;
			}

			$this->num_rows = $i;
		}

		public function row($n = 0) 
		{
			return (isset($this->res[$n]) ? $this->res[$n] : false);
		}
	}

?>